/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package east2west.action;

import east2west.dao.HotelDAO;
import east2west.dto.HotelDTO;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Admin
 */
public class InsertHotelAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
      private static final String ERR = "err";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
         javax.servlet.ServletConfig config = servlet.getServletConfig();
        boolean flag = true;
        ArrayList<HotelDTO> list = new ArrayList<HotelDTO>();
        HotelDTO dto = new HotelDTO();
        HotelDAO dao = new HotelDAO();
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (!isMultipart) {
        } else {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = null;
            try {
                items = upload.parseRequest(request);
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
            Iterator itr = items.iterator();
            while (itr.hasNext()) {
                FileItem item = (FileItem) itr.next();
                if (item.isFormField()) {
                    String name = item.getFieldName();
                    if (name.equals("frhotelname")) {
                        dto.setHotelName(item.getString());
                    }
                    if (name.equals("frhotelemail")) {
                         dto.setEmail(item.getString());
                    }
                    if (name.equals("frhotelweb")) {
//                        int modeId = Integer.parseInt(item.getString());
                        dto.setWebsite(item.getString());
                    }
                    if (name.equals("frhotelrate")) {
                        int rate= Integer.parseInt(item.getString());
                         dto.setRate(rate);
                    }
                    if (name.equals("frhoteladd")) {
                        dto.setAddress(item.getString());
                    }
                    if (name.equals("frhotelphone")) {
                        dto.setPhone(item.getString());
                    }
                    if (name.equals("frhotelcity")) {
                         dto.setCity(item.getString());
                    }
                    if (name.equals("frhoteldes")) {

                         dto.setDescription(item.getString());
                    }

                } else {
                    try {
                        String itemName = item.getName();
                        dto.setImagesUrl("images/Hotel/" + itemName);
                        list.add(dto);
                        String url = getServlet().getServletContext().getRealPath("/");
                        String url1 = url.substring(0, (url.length() - 10));
                        File savedFile = new File(url1 + "/web/images/Hotel/" + itemName);
                        item.write(savedFile);
                       // request.setAttribute("url", url1);

                    } catch (Exception e) {
                        flag = false;
                    }
                }
            }
        }
         boolean flagInsert = true;
        if (flag) {
            int hotelId = dao.create(dto);
            if (hotelId > 0) {
                for (Iterator<HotelDTO> it = list.iterator(); it.hasNext();) {
                    dto = it.next();
                    dto.setHotelId(hotelId);

                }

            }
        }
//                dto.setHotelName(request.getParameter("frhotelname"));
//                dto.setEmail(request.getParameter("frhotelemail"));
//                dto.setWebsite(request.getParameter("frhotelweb"));
//                dto.setImagesUrl(request.getParameter("frhotelimage"));
//                dto.setRate(Integer.parseInt(request.getParameter("frhotelrate")));
//                dto.setAddress(request.getParameter("frhoteladd"));
//                dto.setPhone(request.getParameter("frhotelphone"));
//                dto.setCity(request.getParameter("frhotelcity"));
//                dto.setDescription(request.getParameter("frhoteldes"));
                //dto.setIsDeleted(request.getParameter("isDeleted"));
              
                    return mapping.findForward(SUCCESS);

    }
}

